//
//  GSAboutViewController.m
//  AgileNYC
//
//  Created by German on 04.08.14.
//  Copyright (c) 2014 Softlegion. All rights reserved.
//

#import "SGSAboutViewController.h"
#import "SGSDelegatesManager.h"
#import "SGSModelsManager.h"
#import "SGSDialogsManager.h"
#import "SGSUtils.h"
#import "SGSAppConsts.h"

@interface SGSAboutViewController ()

@end

@implementation SGSAboutViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadSessionID];
}

- (IBAction)onContinueButtonClick:(UIButton *)sender {
    //[self.navigationController popViewControllerAnimated:YES];
    
    SGSDelegatesManager * delegatesManager = [SGSDelegatesManager sharedDelegatesManager];
    
    if (delegatesManager.currentEventsVC) {
        [self.navigationController popToViewController:delegatesManager.currentEventsVC animated:YES];
    } else {
        UIViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"SGSEventsViewController"];
        
        [self.navigationController pushViewController:vc animated:YES];
    }
}


- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}





- (IBAction)onConnectButtonClick:(UIButton *)sender {
   //[self checkLoginAndMakeConnection];
   [[SGSDelegatesManager sharedDelegatesManager].currentEventsVC showConnectionDialog];
}

-(void) loadSessionID {
    NSString * sessionID = [[SGSModelsManager sharedModelsManager] currentUser].sessionID;
    
    if ((sessionID) && (![sessionID isEqualToString:@"0"]) && (![sessionID isEqualToString:@"(null)"])) {
        self.labSessionId.text = sessionID;
        self.labSessionId.hidden = NO;
    } else {
        self.labSessionId.hidden = YES;
    }
}

-(void) checkLoginAndMakeConnection {
    SGSModelsManager * modelsManager = [SGSModelsManager sharedModelsManager];
    
    if (![modelsManager checkUserLogin]) {
        
        UIViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"SGSLoginViewController"];
        [self.navigationController pushViewController:vc animated:YES];
        
    } else {
        [[SGSDialogsManager dialogsManager] showConnectionDialog:@""
              onComplete:^(NSInteger buttonIndex, NSString *returnText) {
                  GSLog(GSLogInfo, @"[SGSAboutViewController] Connection dialog. Button pressed: %d. Returned: %@", buttonIndex, returnText);
                  
                  switch (buttonIndex) {
                      case 0: // Connect or Accept
                          [[SGSModelsManager sharedModelsManager] sendConnectionRequest:returnText onComplete:^(BOOL result, NSError * error) {
                              if (!result) {
                                  [[SGSDialogsManager dialogsManager] showSimpleDialog:Error_Dialog_Title message:[error domain] onComplete:nil];
                              }
                          }];
                          break;
                      case 1: //Cancel or Decline
                          GSLog(GSLogWarning, @"[SGSAboutViewController] Connection cancelled");
                          break;
                      default: GSLog(GSLogWarning, @"[SGSAboutViewController] Unknown button pressed");
                  }
                  
              }];
    }
}


@end
